package com.xiaoshuidi.cloud.module.finance.controller.app.finance;

import com.xiaoshuidi.cloud.framework.common.core.KeyValue;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceBankAreaDO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceBankDO;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "租客 APP - 银行信息")
@RestController("appFinanceBankController")
@RequestMapping("/finance/bank")
public class AppFinanceBankController {
    @Resource
    private FinanceBankService financeBankService;

    @GetMapping("/area")
    @Operation(summary = "获取银行省市列表")
    @Parameter(name = "parentId", description = "父id(查询省时传0, 查询市时传省的id)", required = true, example = "0")
    @Parameter(name = "type", description = "1省/2市", required = true, example = "1")
    public CommonResult<List<KeyValue<Integer, String>>> area(@RequestParam("parentId") Integer parentId,
                                                              @RequestParam("type") Integer type) {
        List<FinanceBankAreaDO> list = financeBankService.getBankAreaList(type, parentId);
        return CommonResult.success(list.stream().map(v -> new KeyValue<>(v.getId(), v.getName())).collect(Collectors.toList()));
    }

    @GetMapping("/list-by-name")
    @Operation(summary = "支行名称模糊查询")
    @Parameter(name = "province", description = "省名称", required = false, example = "北京市")
    @Parameter(name = "city", description = "市名称", required = false, example = "北京市")
    @Parameter(name = "name", description = "支行名", required = true, example = "工行")
    public CommonResult<List<FinanceBankDO>> bankByName(@RequestParam(value = "province", required = false) String province,
                                                                   @RequestParam(value = "city", required = false) String city,
                                                                   @RequestParam(value = "name", required = false) String name
    ) {
        List<FinanceBankDO> list = financeBankService.getBankPage(province, city, name);
        return CommonResult.success(list);
    }
}
